home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / stdio / filbuf.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  1KB  |  68 lines

  1.  
  2. /*
  3.  *  _FILBUF.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <errno.h>
  14.  
  15. int
  16. _filbuf(fi)
  17. FILE *fi;
  18. {
  19.     int n;
  20.     int error = 0;
  21.  
  22.     chkabort();
  23.     if (fi && (fi->sd_Flags & __SIF_OPEN) && fi->sd_RLeft <= 0) {
  24.     /*
  25.      *  if stdin flush stdout
  26.      */
  27.     if (fi == stdin)
  28.         fflush(stdout);
  29.     /*
  30.      *  if a normal file flush pending write data
  31.      */
  32.     if (fi->sd_WLeft >= 0 && (fi->sd_Flags & __SIF_FILE)) {
  33.         fflush(fi);
  34.         fi->sd_WLeft = -1;
  35.         fi->sd_RLeft = 0;
  36.     }
  37.     if (fi->sd_BufSiz && fi->sd_RBuf == NULL) {
  38.         fi->sd_RBuf = malloc(fi->sd_BufSiz);
  39.         fi->sd_Flags |= __SIF_MYBUF;
  40.         if (fi->sd_RBuf == NULL)     /*  if couldn't malloc, make unbuf'd */
  41.         fi->sd_BufSiz = 0;
  42.     }
  43.     if (fi->sd_BufSiz) {            /*  else we might be unbuffered!     */
  44.         n = read(fi->sd_Fd, fi->sd_RBuf, fi->sd_BufSiz);
  45.         if (n < 0) {
  46.         if (errno != EAGAIN) {
  47.             fi->sd_Error = EOF;
  48.             error = EOF;
  49.         }
  50.         } else {
  51.         fi->sd_RLeft = n;
  52.         fi->sd_Offset += n;
  53.         if (n == 0) {
  54.             error = EOF;
  55.             fi->sd_Flags |= __SIF_EOF;
  56.         } else {
  57.             fi->sd_Flags &= ~__SIF_EOF;
  58.         }
  59.         }
  60.     } else {
  61.         fi->sd_RLeft = 0;    /*  unbuffered */
  62.     }
  63.     fi->sd_RPtr = fi->sd_RBuf;
  64.     }
  65.     return(error);
  66. }
  67.  
  68.